from qiniu import Auth,put_data

access_key = "5436kpDr0CZE43OYBjKqfspZ692OibecLgFwlqQx"
secret_key = "f_KBewvBLH46l1iPbBbqx1XV4iGR3os8Egustbgg"
bucket_name = "xixi"


def storage(data):
    if not data:
        return None
    try:
        # 构建鉴权对象
        q = Auth(access_key, secret_key)

        # 生成上传 Token，可以指定过期时间等
        token = q.upload_token(bucket_name)

        # 上传文件
        ret, info = put_data(token, None, data)

    except Exception as e:
        raise e
    print()
    if info and info.status_code != 200:
        raise Exception("上传文件到七牛失败")
    return ret["key"]

if __name__ == '__main__':
    file=input("请输入文件路径")
    with open(file, "rb") as f:
        storage(f.read())
